package com.example.xd.defaultcall.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xd.defaultcall.R;

/**
 * @auther XuDan on 2020/6/29.
 */
public class DialpadChooserAdapter extends BaseAdapter {

    // IDs for the possible "choices":
    public static final int DIALPAD_CHOICE_USE_DTMF_DIALPAD = 101;
    public static final int DIALPAD_CHOICE_RETURN_TO_CALL = 102;
    public static final int DIALPAD_CHOICE_ADD_NEW_CALL = 103;
    private static final int NUM_ITEMS = 12;
    private LayoutInflater inflater;
    private ChoiceItem[] choiceItems = new ChoiceItem[NUM_ITEMS];

    public DialpadChooserAdapter(Context context) {
        inflater = LayoutInflater.from(context);

        choiceItems[0] =
                new ChoiceItem(
                        context.getString(R.string.dialer_useDtmfDialpad),
                        BitmapFactory.decodeResource(
                                context.getResources(), R.drawable.ic_dialer_fork_tt_keypad),
                        DIALPAD_CHOICE_USE_DTMF_DIALPAD);

        // - "Return to call in progress"
        choiceItems[1] =
                new ChoiceItem(
                        context.getString(R.string.dialer_returnToInCallScreen),
                        BitmapFactory.decodeResource(
                                context.getResources(), R.drawable.ic_dialer_fork_current_call),
                        DIALPAD_CHOICE_RETURN_TO_CALL);

        // - "Add call"
        choiceItems[2] =
                new ChoiceItem(
                        context.getString(R.string.dialer_addAnotherCall),
                        BitmapFactory.decodeResource(
                                context.getResources(), R.drawable.ic_dialer_fork_add_call),
                        DIALPAD_CHOICE_ADD_NEW_CALL);
        choiceItems[3] =
                new ChoiceItem(
                        context.getString(R.string.dialer_useDtmfDialpad),
                        BitmapFactory.decodeResource(
                                context.getResources(), R.drawable.ic_dialer_fork_tt_keypad),
                        DIALPAD_CHOICE_USE_DTMF_DIALPAD);
        choiceItems[4] =
                new ChoiceItem(
                        context.getString(R.string.dialer_useDtmfDialpad),
                        BitmapFactory.decodeResource(
                                context.getResources(), R.drawable.ic_dialer_fork_tt_keypad),
                        DIALPAD_CHOICE_USE_DTMF_DIALPAD);
        choiceItems[5] =
                new ChoiceItem(
                        context.getString(R.string.dialer_useDtmfDialpad),
                        BitmapFactory.decodeResource(
                                context.getResources(), R.drawable.ic_dialer_fork_tt_keypad),
                        DIALPAD_CHOICE_USE_DTMF_DIALPAD);
        choiceItems[6] =
                new ChoiceItem(
                        context.getString(R.string.dialer_useDtmfDialpad),
                        BitmapFactory.decodeResource(
                                context.getResources(), R.drawable.ic_dialer_fork_tt_keypad),
                        DIALPAD_CHOICE_USE_DTMF_DIALPAD);
        choiceItems[7] =
                new ChoiceItem(
                        context.getString(R.string.dialer_useDtmfDialpad),
                        BitmapFactory.decodeResource(
                                context.getResources(), R.drawable.ic_dialer_fork_tt_keypad),
                        DIALPAD_CHOICE_USE_DTMF_DIALPAD);
        choiceItems[8] =
                new ChoiceItem(
                        context.getString(R.string.dialer_useDtmfDialpad),
                        BitmapFactory.decodeResource(
                                context.getResources(), R.drawable.ic_dialer_fork_tt_keypad),
                        DIALPAD_CHOICE_USE_DTMF_DIALPAD);
        choiceItems[9] =
                new ChoiceItem(
                        context.getString(R.string.dialer_useDtmfDialpad),
                        BitmapFactory.decodeResource(
                                context.getResources(), R.drawable.ic_dialer_fork_tt_keypad),
                        DIALPAD_CHOICE_USE_DTMF_DIALPAD);
        choiceItems[10] =
                new ChoiceItem(
                        context.getString(R.string.dialer_useDtmfDialpad),
                        BitmapFactory.decodeResource(
                                context.getResources(), R.drawable.ic_dialer_fork_tt_keypad),
                        DIALPAD_CHOICE_USE_DTMF_DIALPAD);
        choiceItems[11] =
                new ChoiceItem(
                        context.getString(R.string.dialer_useDtmfDialpad),
                        BitmapFactory.decodeResource(
                                context.getResources(), R.drawable.ic_dialer_fork_tt_keypad),
                        DIALPAD_CHOICE_USE_DTMF_DIALPAD);
    }

    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

    /**
     * Return the ChoiceItem for a given position.
     */
    @Override
    public Object getItem(int position) {
        return choiceItems[position];
    }

    /**
     * Return a unique ID for each possible choice.
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * Make a view for each row.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // When convertView is non-null, we can reuse it (there's no need
        // to reinflate it.)
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.dialpad_chooser_list_item, null);
        }

        TextView text = convertView.findViewById(R.id.text);
        text.setText(choiceItems[position].text);

        ImageView icon = convertView.findViewById(R.id.icon);
        icon.setImageBitmap(choiceItems[position].icon);

        return convertView;
    }

    // Simple struct for a single "choice" item.
    public static class ChoiceItem {

        String text;
        Bitmap icon;
        public int id;

        ChoiceItem(String s, Bitmap b, int i) {
            text = s;
            icon = b;
            id = i;
        }
    }

}
